package com.gitee.aurora.agent.dao.impl;

import com.balloon.springboot.datasource.aop.DataSourceRouteKey;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gitee.aurora.agent.dao.ZfjdCheckDao;
import com.gitee.aurora.agent.vo.DataCheckVo;
import com.gitee.aurora.core.comm.AuroraJdbcTemplate;
import com.gitee.aurora.core.comm.AuroraNamedParameterJdbcTemplate;
import com.gitee.aurora.core.utils.MapToObj;
import com.gitee.aurora.core.utils.ResultVO;
import com.gitee.aurora.core.utils.ResultVOUtils;
import com.gitee.aurora.core.utils.StringProcessUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * author: jiangfuxiao
 * Date:2022/9/2 11:47
 */
@Repository("zfjdCheckDaoImpl")
class ZfjdCheckDaoImpl implements ZfjdCheckDao {

    @Autowired
    private AuroraJdbcTemplate auroraJdbcTemplate;

    @Autowired
    private AuroraNamedParameterJdbcTemplate jdbcTemplate;

    /**
     * PO路径
     */
    private static final String PO_PATH = "com.gitee.aurora.agent.po.";

    @Override
    public List<Map<String, Object>> queryTableDataByIds(DataCheckVo vo) {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> args = new HashMap<>();
        if (CollectionUtils.isEmpty(vo.getIds())) {
            return list;
        }
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT S_ID AS sId FROM ");
        sql.append(vo.getTableName());
        sql.append("  WHERE 1=1 ");
        args.put("ids", vo.getIds());
        sql.append("  AND S_ID IN (:ids) ");
        try {
            list = jdbcTemplate.queryForList(sql.toString(), args);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }

    @Override
    public List<String> queryTableDataByCode(String tableName) {
        List<String> list = new ArrayList<>();
        StringBuilder builder = new StringBuilder();
        builder.append(" SELECT  DATE_FORMAT(MAX(GXSJ),\"%Y-%m-%d %H:%i:%S\") GXSJ FROM ");
        builder.append(tableName);
        try {
            list = auroraJdbcTemplate.queryForList(builder.toString(), String.class);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }

    @Override
    public ResultVO insertData(DataCheckVo vo) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            String code = StringProcessUtils.firstCodeUpperConvertJavaHump(vo.getTableName().toLowerCase());
            List<LinkedHashMap<String, Object>> dataList = (List<LinkedHashMap<String, Object>>) mapper.readValue(vo.getListStr(), List.class);
            dataList.forEach((v -> {
                if (!v.containsKey("sId")) {
                    v.put("sId", v.get("S_ID"));
                }
            }));
            List resultList = new ArrayList();
            for (Object o : dataList) {
                resultList.add(MapToObj.mapUpperCaseFieldToObj((Map) o, Class.forName(PO_PATH + code)));
            }
            int[] ints = auroraJdbcTemplate.batchInsertForTableName(auroraJdbcTemplate.getDataSource(), vo.getTableName(), resultList);
            return ResultVOUtils.success(ints);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVOUtils.error("批量插入执行错误");
        }
    }

    @Override
    @DataSourceRouteKey(type = "secondMysqlDB")
    public List<Map<String, Object>> queryTable() {
        List<Map<String, Object>> list = new ArrayList<>();
        StringBuilder builder = new StringBuilder();
        builder.append(" SELECT  TABLE_CODE AS tabCode, TABLE_CODE_NAME AS tabName  FROM  TAB_TABLE_CONFIG");
        try {
            list = auroraJdbcTemplate.queryForList(builder.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }

}
